package com.android.jblm.commandcentral.generators;

import android.content.Context;
import android.content.res.Resources;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.android.jblm.commandcentral.R;
import com.android.jblm.commandcentral.objects.Category;
import com.android.jblm.commandcentral.objects.Contact;
import com.android.jblm.commandcentral.objects.Directory;
import com.android.jblm.commandcentral.objects.Header;

/**
 * Created by Brian 2014.
 */
public class DirectoryBuilder {

    private Directory book;

    private Context myContext;

    //planned usage for keeping persistent serialized copy of 'book'
    //to avoid recreating it ever time, for now too complex
    private static Boolean makeFile = false;

    public DirectoryBuilder(Context c){
        myContext = c;
        makeFile = true;
        book = new Directory();
        buildBook();
    }

    /*public DirectoryBuilder(Directory theBook){
        book = theBook;
    }*/

    public Directory getBook() {
        return book;
    }

    public  void buildBook() {

        if(makeFile){
            try {

                XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
                XmlPullParser xpp = xppf.newPullParser();
                xppf.setNamespaceAware(true);
                //File myXML = new File(myContext.getFilesDir() + "/" + "jblm-app-directory"); // give proper path
                InputStream fis = myContext.getResources().openRawResource(R.raw.jblmappdirectoryfile);

                xpp.setInput(fis, null);
                int type = xpp.getEventType();
                String flag = "";

                Header curHeader = new Header();
                Category curCategory = new Category();
                Contact curContact = new Contact();

                while(type != XmlPullParser.END_DOCUMENT) {


                    if(type == XmlPullParser.START_TAG) {
                        System.out.println(xpp.getName().toString());
                        System.out.println("=========START-TAG");
                        if(xpp.getName().equals("Header")){
                            flag = "Header";
                        } else if(xpp.getName().equals("Category")){
                            flag = "Category";
                        } else if(xpp.getName().equals("Contact")){
                            curContact = new Contact();

                        } else if(xpp.getName().equals("Name")){
                            flag = "Name";
                        } else if(xpp.getName().equals("Phone")){
                            flag = "Phone";
                        } else if(xpp.getName().equals("URL")){
                            flag = "URL";
                        } else if(xpp.getName().equals("Email")){
                            flag = "Email";
                        } else if(xpp.getName().equals("Description")){
                            flag = "Description";
                        }
                    } else if(type == XmlPullParser.TEXT) {
                        System.out.println("TEXT: "+xpp.getText() + "_TEST");

                        System.out.println("=== " + flag + " ===");

                        if(!xpp.getText().replaceAll("[^A-Za-z0-9]", "").equals("")){

                            if(flag.equals("Header")){
                                curHeader = new Header(xpp.getText().replaceAll("[^A-Za-z0-9]", ""));
                            }else if(flag.equals("Category")){
                                System.out.println("Header: " + curHeader.getName());
                                curCategory = new Category(xpp.getText().replaceAll("[^A-Za-z0-9]", ""));
                                System.out.println("Category: " + curCategory.getTitle());
                            } else if(flag.equals("Name")){
                                curContact.contactName = xpp.getText();
                                System.out.println("Contact " + curContact.contactName);
                            } else if(flag.equals("Phone")){
                                curContact.addNumber(xpp.getText());
                                System.out.println("Phone");
                                System.out.println("Contact " + curContact.contactName);
                            } else if(flag.equals("URL")){
                                System.out.println("URL");
                                System.out.println("Contact" + curContact.contactName.toString());
                                curContact.contactURL = xpp.getText();
                            } else if(flag.equals("Email")){
                                System.out.println("Email");
                                System.out.println("Contact" + curContact.contactName);
                                curContact.contactEmail = xpp.getText();
                            } else if(flag.equals("Description")){
                                System.out.println("Desc");
                                System.out.println("Contact" + curContact.contactName);
                                curContact.contactDesc = xpp.getText();
                            }

                        }



                    } else if(type == XmlPullParser.END_TAG) {

                        System.out.println("END: "+xpp.getName().toString());

                        if(xpp.getName().equals("Header")){
                            book.addHeader(curHeader);
                        }else if(xpp.getName().equals("Category")){
                            curHeader.addCategory(curCategory);
                        } else if(xpp.getName().equals("Contact")){
                            curCategory.addContact(curContact);

                        }

                    }


                    type = xpp.next();
                }


            }
            catch (XmlPullParserException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();

            }
        }
    }


}
